﻿using ImTools;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using RecordBill.Dtos;
using RecordBill.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace RecordBill.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        private IRegionManager regionManager;
        private Dictionary<string, string> HomeMenus;
        public ObservableCollection<UIMenu> NavItems { get; set; }
        public DelegateCommand<string> NavigateCommand { get; set; }
        public DelegateCommand<string> CloseNavItemCommand { get; set; }
        public DelegateCommand<string> NavCheckCommand { get; set; }
        
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public MainWindowViewModel(IRegionManager rmanager)
        {

            InitHomeMenus();
            NavItems = new ObservableCollection<UIMenu>();
            NavItems.Add(new UIMenu()
            {
                NavTitle = "首页",
                NavUrl = "Home",
                IsChecked = true
            });
            //NavItems.Add(new UIMenu() { NavTitle = "首页", NavId = 1, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页1", NavId = 2, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页2", NavId = 3, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页3", NavId = 4, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页4", NavId = 5, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页5", NavId = 6, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页6", NavId = 7, NavIcon = "Home" });
            //NavItems.Add(new UIMenu() { NavTitle = "首页7", NavId = 8, NavIcon = "Home" });
            this.regionManager = rmanager;
            NavigateCommand = new DelegateCommand<string>(Navigate);
            CloseNavItemCommand = new DelegateCommand<string>(CloseNavItem);
            NavCheckCommand = new DelegateCommand<string>(NavCheck);


        }
        private void InitHomeMenus()
        {
            HomeMenus = new Dictionary<string, string>();
            HomeMenus.Add("Home", "首页");
            HomeMenus.Add("BillStatisticsView", "账单统计");
            HomeMenus.Add("BillTypeView", "分类管理");
            HomeMenus.Add("SettView", "设置");
        }

        private void NavCheck(string navTitle)
        {
            var find = NavItems.FindFirst(a => a.NavTitle == navTitle);
            if (find?.IsChecked == true)
            {
                return;
            }
            else if(find?.IsChecked==false)
            {
                foreach (var item in NavItems)
                {
                    item.IsChecked = false;
                }
                Navigate(find.NavUrl);
            }
        }

        private void CloseNavItem(string title)
        {
            var find = NavItems.FindFirst(a => a.NavTitle == title);
            if (find != null)
            {
                var index = NavItems.IndexOf(find);

                if (index == 0)
                {
                    if (NavItems.Count == 1)
                    {
                        if (find.NavUrl != "Home")
                        {
                            Navigate("Home");
                            NavItems.Remove(find);
                        }

                    }
                    else
                    {
                        Navigate(NavItems[index + 1].NavUrl);
                        NavItems.Remove(find);
                    }
                }
                else
                {
                    Navigate(NavItems[index - 1].NavUrl);
                    NavItems.Remove(find);
                }

            }
        }
        private void Navigate(string obj)
        {
            var navTitle = HomeMenus[obj];
            var find = NavItems.FindFirst(a => a.NavTitle == navTitle);
            foreach (var item in NavItems)
            {
                item.IsChecked = false;
            }
            if (find == null)
            {
                find = new UIMenu() { NavUrl = obj, IsChecked = true, NavTitle = navTitle, NavIcon = obj };
                NavItems.Add(find);
            }
            else
            {
                find.IsChecked = true;
            }

            if ("Home" == obj)
            {
                regionManager.RequestNavigate(PrismManager.RegionName, "HomeView");
            }

            else if ("BillStatisticsView" == obj)
            {
                regionManager.RequestNavigate(PrismManager.RegionName, "BillStatisticsView");
            }
            else if ("BillTypeView" == obj)
            {
                regionManager.RequestNavigate(PrismManager.RegionName, "BillTypeView");
            }
            else if ("SettView" == obj)
            {
                regionManager.RequestNavigate(PrismManager.RegionName, "SettView");
            }
        }
    }
}
